package cn.hnu.qq.global;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.io.File;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;

import org.apache.commons.lang.math.RandomUtils;

import cn.hnu.qq.entity.Crowd;
import cn.hnu.qq.entity.User;

public class CellRenderer extends JLabel implements ListCellRenderer {

	

	public Component getListCellRendererComponent(JList list, Object value,
			int index, boolean isSelected, boolean cellHasFocus) {
		 
			User user ;
			Crowd crowd;
		
	      try {
	    	  if(value instanceof User){
	    		  user = (User) value;
	    		  setIcon(new ImageIcon(ImageIO.read(new File("src/main/java/image/touxiang/"+user.getHeadImage()+".png"))));
	    		  setText(user.getAcount());//设置文本
	    		  setToolTipText(user.getAcount());//设置提示文本
	    	  }else if(value instanceof Crowd){
	    		 
	    		  crowd = (Crowd) value; 
	    		  setText(crowd.getCrowdName());//设置文本
	    		  setToolTipText(crowd.getCrowdName());//设置提示文本
	    		  setIcon(new ImageIcon(ImageIO.read(new File("src/main/java/image/touxiang/"+crowd.getImage()+".png"))));
	    	  }
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}//设置图片
	
	      
	      setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));//加入宽度为5的空白边框
	      
	   
	       if(isSelected)//如果选中
	       {
	        setBackground(Color.cyan);//设置背景色   
	        setForeground(Color.blue);   
	      }   
	      else//没有选中
	      {   
	        setBackground(Color.white);  //设置背景色  
	        setForeground(Color.blue);   
	       }   
	      setEnabled(list.isEnabled());   
	      setFont(new Font("sdf",Font.ROMAN_BASELINE,13));   
	      setOpaque(true);   
	      return this;   
	}

}
